<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>Promise 封装AJAX请求</title>
	</head>
	<body>
	</body>
	<script type="text/javascript">
        
        function sendAJAX(url){

            return new Promise((resolve,reject)=>{

                const xhr = new XMLHttpRequest();

                xhr.responseType="json";
                xhr.open('GET',url);
                xhr.send();

                //处理结果

                xhr.onreadystatechange = function(){

                    if(xhr.readyState === 4){
                        //判断是否成功
                        if(xhr.status >=200& xhr.status<300){
                            resolve(xhr.response);
                            console.log(123)
                            reject(xhr.status);
                        }else{
                            reject(xhr.status);
                        }
                    }

                }

            });

        }

        let promise = sendAJAX('https://api.apiopen.top/getJoke');
        promise.then(value=>{
            console.log(value);
        },reason=>{
            console.warn(reason);
        })
        console.log(promise);

    </script>
</html>